<layout name="layout" />

<!-- 列表 -->
<div class="list-div" id="listDiv">
      <form enctype="multipart/form-data" action="__SELF__" method="post">
	<table cellpadding="3" cellspacing="1">
    	<tr>
           <?php foreach($newshangpinattrData as $k=>$v): ?>
             
            <th ><?php echo $k ;?></th>
              <?php endforeach; ?>
              <th width="200">库存量</th>
			<th width="130">操作</th>
        </tr>
           <?php if($firstData):
           
            foreach($firstData as $k =>$v):
            $str=','.$v['attr_list'].',';
           ?>
         
	<tr>
           <?php foreach($newshangpinattrData as $k1=>$v1): ?>
           <td>
               <select name="attrs[]">
                     <option value="">请选择</option>
             <?php foreach($v1 as $k2=>$v2):
             if(strpos($str,$v2['id']))
                    				$select = 'selected="selected"';
                    			else 
                    				$select = '';
             ?>
              <option <?php echo $select; ?> value="<?php echo $v2['id'] ?>"><?php echo $v2['attr_value'] ?></option>
             <?php endforeach; ?>
                          </select>
 
                       </td>

              <?php endforeach; ?>
              
              <td>
                  <input type="text" value="<?php echo $v['number'];?>" name='nums[]'/> 
              </td>	
              <td>
                  <span  onclick="add(this)" class="add"><?php echo $k==0?"+":"-" ?></span>
              </td>
        </tr>
          <?php endforeach; ?>
                
        <?php else: ?>
        <tr>
           <?php foreach($newshangpinattrData as $k1=>$v1): ?>
           <td>
               <select name="attrs[]">
                     <option value="">请选择</option>
             <?php foreach($v1 as $k2=>$v2):?>
              <option  value="<?php echo $v2['id'] ?>"><?php echo $v2['attr_value'] ?></option>
             <?php endforeach; ?>
                          </select>
 
                       </td>

              <?php endforeach; ?>
              
              <td>
                  <input type="text"  name='nums[]'/> 
              </td>	
              <td>
                  <span  onclick="add(this)" class="add">+</span>
              </td>
        </tr>

         <?php endif;?>
        
        <tr style="text-align: center" id='tijiao' ><td>提交</td></tr>
	</table>
           </form>
</div>

<script>
$(function(){
  $('#tijiao').click(function(){
     $('form').submit();
  });
});
  function add(a){
  
         var tr= $(a).parent().parent();
   if($(a).text()=="+"){
       //clone不会clone事件，但是这里是直接写到dom上去的，所以这里可以用
       var newtr=tr.clone();
       newtr.find('.add').text('-');
       $('#tijiao').before(newtr);
   }else{
       tr.remove();//删除
   }
    }
</script>
